const app = getApp();
var url = app.getURL();

Page({
  data: {
    type: null,
    imgList: [],
    photo:"",
    textarea: '',
    switch: 0
  },

  onLoad:function(options){
    let that = this;
    that.isLogin();//登录验证
  },
// 获取内容
  innerText(e){
    this.setData({
      textarea: e.detail.value
    })
  },
  change: function (event) {
    let swi = event.detail.value
    if(swi==false){
      this.setData({
        switch: 0
      })
    }else{
      this.setData({
        switch: 1
      })
    }
  },
  //提交表单
  getsubmit: function(e) {
  console.log('form发生了submit事件，携带的数据为：', e.detail.value)

    var that = this;
    let tempFilePaths = this.data.imgList
    
    if (e.detail.value.content=='') {
      wx.showToast({
        title: '内容不能为空~',
        icon: "none",
        duration: 2000
      })
    }else {
      if(this.data.imgList!=''){
        console.log("存在照片，上传照片中")
        wx.uploadFile({
          url: url+'/sysUse/sys/sysUse/oss/fileUpload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'folder': 'userImg' 
          },
          success (res){
            var fi = JSON.parse(res.data)//转为对象使用
            console.log(fi)
            that.setData({
              photo: fi.data
            })
            wx.showToast({
              title: '图片上传成功',
              icon: 'success',
              duration: 1000
            })
          }
        })
      }
        console.log("直接上传")
        wx.showToast({
          title: '发布中',
          icon: 'loading',
          duration: 2000
        })
        // 手动设置延迟，防止图片未上传
        setTimeout(function () {
            that.feedBack(e)
        },3000)
          
      }
    
  },

  // 失物招领发布
  feedBack(e){
    // console.log(e)
    let that = this    
    let id = wx.getStorageSync('message').id
    wx.request({
      url: url+'/user/app/cust/auth/querySuggestList',
      method: "POST",
      header: {// 设置请求的 header
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      data:{
        "custId": id,
        "isAnonymous": that.data.switch,
        "content": that.data.textarea,
        "photos": that.data.photo
      },
      success(res) {
        // console.log(res.data.code)
        if(res.data.code==200){
          wx.showToast({
            title: '感谢您的反馈',
            icon: 'success',
            duration: 1000
          })
          setTimeout(function(){
            wx.switchTab({
              url: '../../../index/index'
            })
          },1000)
        }else{
          wx.showToast({
            title: '反馈失败',
            icon: 'error',
            duration: 1000
          })
        }
      }
    })
  },

    //判断是否登录，如果未登录的话就提示登录
    isLogin(){
      let that = this;
    //获取本地缓存的用户信息
   let user = app.getGlobalUserInfo();
      if(user.cookieNickname==null||user.cookieNickname==''){
        wx.showToast({
          title: "请先登录",
          icon: "none",
          duration: 2000
          })
          setTimeout(function(){
            wx.switchTab({
              url: '/pages/my/user/user'
            });
          },600)
        
      }
 },

ChooseImage() {
  wx.chooseImage({
    count: 1, //默认9
    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
    sourceType: ['album'], //从相册选择
    success: (res) => {
      if (this.data.imgList.length != 0) {
        this.setData({
          imgList: this.data.imgList.concat(res.tempFilePaths)
        })
      } else {
        this.setData({
          imgList: res.tempFilePaths
        })
      }
    }
  });
},
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '',
      content: '确定要删除这张照片吗？',
      cancelText: '再看看',
      confirmText: '确认删除',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})